//
//  YJcateDetaViewController.m
//  SnackCourier
//
//  Created by qianfeng01 on 15/7/25.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "YJcateDetaViewController.h"
#import "YJcateclassFollowModel.h"

#import "BaseHeaderFlowLayout.h"
#import "YJCollectionView.h"
#import "YJSalesTopicListdetailsCell.h"


#import "CreateSales.h"

static BOOL onetouch;
static BOOL twotouch;
static BOOL theratouch;


@interface YJcateDetaViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) UIButton *classButton;
@property (nonatomic)NSInteger id;

@property (nonatomic,strong) UIButton *one;
@property (nonatomic,strong) UIButton *two;
@property (nonatomic,strong) UIButton *thera;

@end

@implementation YJcateDetaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentPage =0;
    onetouch=YES;
    twotouch=NO;
    theratouch=NO;
    NSString *url =nil;
    if (self.classModel ) {
        self.id =self.classModel.id;
        
        url=[NSString stringWithFormat:kClassflowerUrl,(long)self.id,kdefault,(long)self.currentPage];
    }else if(self.labmodel){
        self.id =self.labmodel.id;
        url=[NSString stringWithFormat:kClassflowerUrl,(long)self.id,kdefault,(long)self.currentPage];
    }else if(self.key){
        url=[[NSString stringWithFormat:kSearchPath,self.key,kdefault,(long)self.currentPage]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    self.dataArray =[[NSMutableArray alloc] init];
    [self collectionViewUI];
    [self createHttpRequest];
    [self createUrl:url];
}


-(void)createUrl:(NSString *)url{
    NSString *path=[YJHelper getFullPathWithFile:url];
    BOOL isExist=[[NSFileManager defaultManager] fileExistsAtPath:path];
    BOOL isTimeout=[YJHelper isTimeOutWithFile:url timeOut:24*60*60];
    if ((isExist==YES)&&(isTimeout==NO)) {
        NSData *data=[NSData dataWithContentsOfFile:[YJHelper getFullPathWithFile:url]];
        NSDictionary *dicts=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *datadic=dicts[@"data"];
        NSArray *productList=datadic[@"productList"];
        for (NSDictionary *dict in productList) {
            YJcateclassFollowModel *model =[[YJcateclassFollowModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataArray addObject:model];
        }
        [self.collectionView reloadData];
    }
    __weak typeof (self)weakSelf= self;
    [self.manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
           NSDictionary *dicts=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *data=dicts[@"data"];
            NSArray *productList=data[@"productList"];
            for (NSDictionary *dict in productList) {
                YJcateclassFollowModel *model =[[YJcateclassFollowModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [weakSelf.dataArray addObject:model];
            }
            [weakSelf.collectionView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}


-(void)collectionViewUI{
    BaseHeaderFlowLayout *flowLayout =[[BaseHeaderFlowLayout alloc] init];
    flowLayout.scrollDirection =UICollectionViewScrollDirectionVertical;
    
    self.automaticallyAdjustsScrollViewInsets=YES;
    self.collectionView =[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64-55) collectionViewLayout:flowLayout];
    self.collectionView.delegate =self;
    self.collectionView.dataSource=self;
    self.collectionView.backgroundColor =[UIColor colorWithWhite:0.8 alpha:1];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"YJSalesTopicListdetailsCell" bundle:nil] forCellWithReuseIdentifier:@"YJSalesTopicListdetailsCell"];
    [self.collectionView registerClass:[YJCollectionView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YJCollectionView"];
    [self.view addSubview:self.collectionView];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    YJSalesTopicListdetailsCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"YJSalesTopicListdetailsCell" forIndexPath:indexPath];
    YJcateclassFollowModel *model =self.dataArray[indexPath.row];
    [cell showDataWithfollowModel:model];
    cell.backgroundColor=[UIColor whiteColor];
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((self.view.frame.size.width-10)/2.0, 260);
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view =nil;

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        view =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YJCollectionView" forIndexPath:indexPath];
        _one=[[UIButton alloc]initWithFrame:CGRectMake(0,2,self.view.frame.size.width/3.0,26)];
        _two=[[UIButton alloc]initWithFrame:CGRectMake(_one.frame.size.width,2,self.view.frame.size.width/3.0,26)];
        _thera=[[UIButton alloc]initWithFrame:CGRectMake(_two.frame.size.width*2,2,self.view.frame.size.width/3.0,26)];
        [_one addTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];
        [_two addTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];
        [_thera addTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];
        _one.tag=301;
        _two.tag =302;
        _thera.tag=303;
        if (onetouch==YES) {
            [_one setBackgroundImage:[UIImage imageNamed:@"zonghe"] forState:UIControlStateNormal];
        }else if(onetouch ==NO){
            [_one setBackgroundImage:[UIImage imageNamed:@"zonghe_1"] forState:UIControlStateNormal];
        }
        if (twotouch==YES){
            [_two setBackgroundImage:[UIImage imageNamed:@"rexiao"] forState:UIControlStateNormal];
        }else if(twotouch ==NO){
            [_two setBackgroundImage:[UIImage imageNamed:@"rexiao_1"] forState:UIControlStateNormal];
        }
        if (theratouch==YES){
            [_thera setBackgroundImage:[UIImage imageNamed:@"jiage"] forState:UIControlStateNormal];
        }else if (theratouch==NO){
            [_thera setBackgroundImage:[UIImage imageNamed:@"jiage_1"] forState:UIControlStateNormal];
        }
        [view addSubview:_one];
        [view addSubview:_two];
        [view addSubview:_thera];
        view.backgroundColor =[UIColor colorWithWhite:0.8 alpha:1];
    }
    return view;
}

-(void)setClassButton:(UIButton *)classButton{
    if (_classButton ==classButton) {
        return;
    }
    _classButton.selected =NO;
    _classButton =classButton;
    _classButton.selected =YES;
}

-(void)clickbtn:(UIButton *)button {
    switch (button.tag) {
        case 301:
        {
            onetouch=YES;
            twotouch=NO;
            theratouch=NO;
            NSString *url =nil;
            if (self.key) {
                url =[[NSString stringWithFormat:kSearchPath,self.key,kdefault,(long)self.currentPage] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            }else{
                url =[NSString stringWithFormat:kClassflowerUrl,(long)self.id,kdefault,(long)self.currentPage];
            }

           
            [self.dataArray removeAllObjects];
            [self.collectionView reloadData];
            [self createUrl:url];
        }
            break;
        case 302:
        {
            onetouch=NO;
            twotouch=YES;
            theratouch=NO;
            NSString *url =nil;
            if (self.key) {
                url =[[NSString stringWithFormat:kSearchPath,self.key,kdefault,(long)self.currentPage] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            }else{
                url =[NSString stringWithFormat:kClassflowerUrl,(long)self.id,kdefault,(long)self.currentPage];
            }
            [self.dataArray removeAllObjects];
            [self.collectionView reloadData];
            [self createUrl:url];
        }
            break;
        case 303:
        {
            onetouch=NO;
            twotouch=NO;
            theratouch=YES;
            NSString *url =nil;
            if (self.key) {
                url =[[NSString stringWithFormat:kSearchPath,self.key,kdefault,(long)self.currentPage] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            }else{
                url =[NSString stringWithFormat:kClassflowerUrl,(long)self.id,kdefault,(long)self.currentPage];
            }
            [self.dataArray removeAllObjects];
            [self.collectionView reloadData];
            [self createUrl:url];
        }
            break;
            
        default:
            break;
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(0, 30);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    YJSalesTopicListdetailsModel *model =self.dataArray[indexPath.row];
    YJImmediatelygrabController *controller =[CreateSales createVC:model.id];
    controller.title=@"商品详情";
    [self.navigationController pushViewController:controller animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
